package com.zp.qualitypractice.io;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.bumptech.glide.load.Key;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import java.security.MessageDigest;

import static android.R.attr.radius;

/**
 * Created by izp on 2017/6/26.
 */

public class GlideTransform {
    static class RoundCornerTransform extends BitmapTransformation{
//        ID完全自定义，可以是任何字符串。正常的是以程序包名开头，加上版本号
        private static final int VERSION = 1;
        private static final String ID = "com.bumptech.glide.load.resource.bitmap.RoundCornerTransform." + VERSION;
        private static final byte[] ID_BYTES = ID.getBytes(Key.CHARSET);
        private static float mRadius = 0f;
        /**
         * 构造函数 默认圆角半径 4dp
         *
         * @param context Context
         */
        public RoundCornerTransform(Context context) {
            this(context,4);
        }
        /**
         * 构造函数
         *
         * @param context Context
         * @param dp      圆角半径
         */
        public RoundCornerTransform(Context context, int dp) {
            super(context);
            this.mRadius = Resources.getSystem().getDisplayMetrics().density * dp;//貌似这个dp转换方法还是不错啊，利用像素密度
        }

        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            return roundCrop(pool, toTransform);
        }

        private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
            if (source == null) return null;
//            从bitmap池中获取同样宽高和颜色配置的bitmap对象
            Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
//            如果池子中没有就创建一个。
            if (result == null) {
                result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            }
//            根据bitmap配置，画画布
            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
//            配置画笔的着色器，这里传入原来需要绘制出的bitmap对象
            paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);//色值抗锯齿
            RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, paint);
            return result;
        }

        /**
         * @param messageDigest
         * 图片转换闪动问题就靠这个解决。
         */
        @Override
        public void updateDiskCacheKey(MessageDigest messageDigest) {
            messageDigest.update(ID_BYTES);
        }

        /**
         * @param o
         * @return
         * 后面这些可覆写，可不覆写，对一般使用和显示效果没影响。但是为了避免被其它转换覆盖，还是覆写一下。
         */
        @Override
        public boolean equals(Object o) {
            return o instanceof RoundCornerTransform;
        }

        @Override
        public int hashCode() {
            return ID.hashCode();
        }
    }
}
